﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Browser;
using Infrastructure.Assets.Resources;
using MVVMBasicRIALibrary.Web.Services;

namespace MVVMBasic.View.Login
{
    public partial class ActivationWindow
    {
        private readonly BasicUserRegistrationContext userRegistrationContext = new BasicUserRegistrationContext();

        public ActivationWindow()
        {
            InitializeComponent();
            this.Loaded += ActivationWindow_Loaded;
        }

        void ActivationWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var userid = HtmlPage.Document.QueryString["actcode"];
            userRegistrationContext.ActivateUser(userid, ActivationOperation_Completed, null);
        }

        private void ActivationOperation_Completed(InvokeOperation<CreateUserStatus> operation)
        {
            if (!operation.IsCanceled)
            {
                if (operation.HasError)
                {
                    lblAstivationStatus.Text = operation.Error.Message;
                }
                else 
                  switch (operation.Value)
                  {
                      case CreateUserStatus.Success:
                          lblAstivationStatus.Text = ApplicationStrings.AccountActivationSucceed;
                          break;
                      case CreateUserStatus.InvalidPassword:
                          lblAstivationStatus.Text = ApplicationStrings.AccountActivationBadData;
                          break;
                      case CreateUserStatus.Failure:
                          lblAstivationStatus.Text = ApplicationStrings.AccountAlreadyActivated;
                          break;
                  }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

